//倒计时
(function($){
	if(typeof $ == "undefined"){
        console.log('No Jquery Included.');
        return false;
    }
	$.fn.extend({
		timer: function(options){
			var defaults = {
				now: Math.round(new Date().getTime()/1000),
				count: 10,      
				offset: 0,
				overtime: _callback,
			};
			options = $.extend(defaults, options);
			
			var _this = $(this);
			options.offset = 0 === options.offset ? Math.round(new Date().getTime()/1000) : options.offset;
			_this.timer = null;
			_this.timer = setInterval(counter, 1000);
			counter();
			
			function counter(){
				var timestamp  = options.now ++;
				var offsetTime = options.offset;
				var expiretime = offsetTime + options.count;
				if( timestamp > expiretime ){
					clearInterval(_this.timer);
					if( 'function' === typeof options.overtime ) options.overtime(_this);
				}else{
					var rest = expiretime - timestamp;
					var r = exchange(rest);
					_this.html(r.d+'天'+r.h+'小时'+r.m+'分'+r.s+'秒');
				}
			}
			
			function _callback(){
				_this.html('已超时');
			}
			
			//时间转换		
			function exchange(rest){
				var r = {d: 0, h: 0, m: 0, s: 0};
				if( rest < 60 ){
					r.s = rest;
					return r;
				}
				
				if(rest >= 60 && rest < 3600  ){
					r.m = parseInt(rest / 60);
					r.s = rest % 60;
					return r;
				}
				
				if(rest >= 3600 && rest < 86400  ){
					r.h = parseInt(rest / 3600);
					var v = exchange(rest % 3600);
					r.m = v.m;
					r.s = v.s;
					return r;
				}
				
				if(rest >= 86400 ){
					r.d = parseInt(rest / 86400);
					var v = exchange(rest % 86400);
					r.h = v.h;
					r.m = v.m;
					r.s = v.s;
					return r;
				}
				
				return r;
			}
		}
	})
})(jQuery);